package com.test.dababy.dababy_00_http;

import static com.test.dababy.dababy_00_path.Paths_dababy.PATH_DELETE_BOUND_BANK_ACCOUNT;
import static com.test.dababy.dababy_00_path.Paths_dababy.PATH_LOAN_LIMIT;
import static com.test.dababy.dababy_00_path.Paths_dababy.PATH_UPLOAD_IMAGE_FILE;
import static com.test.dababy.dababy_00_utils.DababyUtil.decryptDefault_dababy;
import static com.test.dababy.dababy_00_utils.DababyUtil.encryptDefault_dababy;
import static com.test.dababy.dababy_00_utils.DababyUtil.safeRun;

import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.test.dababy.OnLoginFailListener;
import com.test.dababy.dababy_00_analytics.dababyevent.DababyIAnalyticsEvent;
import com.test.dababy.dababy_00_analytics.DababyUtil.DababyAnalyticsUtil;
import com.test.dababy.R;
import com.test.dababy.dababy_01_launcher.Base_dababyApplication;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_05_process.Pro_dababycessIn_dababyfo;
import com.test.dababy.dababy_06_loan_apply.LoanApplyDetail_OldUser;
import com.test.dababy.dababy_07_loan_status.Rate_dababyWind_dababyowConfig;
import com.test.dababy.dababy_05_process.Acc_dababyountLi_dababyst_Fr;
import com.test.dababy.dababy_06_loan_apply.LoanApplyDetail;
import com.test.dababy.dababy_04_home.LnLi_dababymitInfo;
import com.test.dababy.dababy_03_login.Log_dababyinInfo;
import com.test.dababy.Othe_dababyrsInfo;
import com.test.dababy.dababy_10_vip.Pers_dababyonalIn_dababyfo;
import com.test.dababy.dababy_08_loan_repay.RpI_dababynf_dababyo;
import com.test.dababy.dababy_00_utils.DababyConfigs;
import com.test.dababy.dababy_00_path.Paths_dababy;
import com.test.dababy.dababy_00_utils.DababyUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;


public class OkHttpUtil {

    private double lS2X=0.6659534600139028;
    private double t40D=0.7009534522489984;
    private float qanJ=0.74348336f;
    private double lP6X=0.669341282232554;
    private int p4d3=175;
    private String y1X4="";

    public void j5Bu2(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getY1X4()))
                    setY1X4(String.valueOf(getlS2X())+getT40D()+getQanJ()+getlP6X()+getP4d3());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setlS2X(double lS2X) {
        this.lS2X = lS2X;
    }

    public String getY1X4() {
        return y1X4;
    }

    public void setY1X4(String y1X4) {
        this.y1X4 = y1X4;
    }

    public void setT40D(double t40D) {
        this.t40D = t40D;
    }

    public void setQanJ(float qanJ) {
        this.qanJ = qanJ;
    }

    public void setlP6X(double lP6X) {
        this.lP6X = lP6X;
    }

    public void setP4d3(int p4d3) {
        this.p4d3 = p4d3;
    }

    public double getlS2X() {
        return lS2X;
    }

    public double getT40D() {
        return t40D;
    }

    public float getQanJ() {
        return qanJ;
    }

    public double getlP6X() {
        return lP6X;
    }

    public int getP4d3() {
        return p4d3;
    }

    public interface ResponseListener_dababy {
        void onResponse(JSONObject responseJson);
        void onResponseFail();
    }

    private static OkHttpClient Client;

    private static final HttpLoggingInterceptor mHttpLoggingInterceptor_dababy = new HttpLoggingInterceptor(new Http_dababyLogger());


    private OkHttpUtil() {

        if (DababyConfigs.Debug) mHttpLoggingInterceptor_dababy.setLevel(HttpLoggingInterceptor.Level.BODY);

        Client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .addNetworkInterceptor(mHttpLoggingInterceptor_dababy)
                .addInterceptor(new Req_dababyInterceptor())
                .callTimeout(5000, TimeUnit.MILLISECONDS)
                .build();
    }

    private static class Inner_dababy {
        private static final OkHttpUtil instance_dababy = new OkHttpUtil();
    }

    public static OkHttpUtil getInstance_dababy() {
        return Inner_dababy.instance_dababy;
    }

    public void swToastBySafeWay(CharSequence text) {
        safeRun(() -> Toast.makeText(Base_dababyApplication.getInstant(),text,Toast.LENGTH_SHORT).show());
    }

    //发送请求
    private void request(String body, ResponseListener_dababy responseListener) {
        if (DababyUtil.isFrontEmpty_dababy()) return;
        String commonPath = DababyUtil.getCommonPath_dababy();
        if (TextUtils.isEmpty(commonPath)) return;
        new Thread(() -> {

            Request request = new Request.Builder()
                    .url(commonPath)
                    .header("product", DababyConfigs.dababyProduct)
                    .post(RequestBody.Companion.create(body, MediaType.Companion.parse("*/*;charset=utf-8")))
                    .build();

            Client.newCall(request).enqueue(
                    //响应
                    new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.request_fail));
                            if (responseListener!=null) responseListener.onResponseFail();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String body_src = response.body().string();
                            new Thread(() -> {
                                String body = decryptDefault_dababy(body_src);
                                if (responseListener!=null) {
                                    try {
                                        JSONObject responseJson = new JSONObject(body);
                                        responseListener.onResponse(responseJson);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                        swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.request_fail));
                                        responseListener.onResponseFail();
                                    }
                                }
                            }).start();
                        }
                    }
            );

        }).start();
    }

    private String parseDefaultParam(String path, JSONObject jsonObject) {
        if (jsonObject==null) jsonObject = new JSONObject();
        path = path.replaceAll("/", "");
        try {
            jsonObject.put("path", path);
            jsonObject.put("isLimitAdTrackingEnabled", DababyAnalyticsUtil.INSTANCE.getAdEnabled_dababy());
            jsonObject.put("aduid", DababyAnalyticsUtil.INSTANCE.getAdId_dababy());
            jsonObject.put("aduidPath", DababyAnalyticsUtil.INSTANCE.getAndroidId_dababy());
            jsonObject.put("version", DababyConfigs.dababyVERSION.trim());
            jsonObject.put("token", DababyUtil.getToken_dababy().trim());
            jsonObject.put("androidId", DababyAnalyticsUtil.INSTANCE.getAndroidId_dababy());
            jsonObject.put("userId", DababyUtil.getUserId_dababy().trim());
            jsonObject.put("advertId", DababyAnalyticsUtil.INSTANCE.getAdId_dababy());
            jsonObject.put("product", DababyConfigs.dababyProduct.trim());
            jsonObject.put("clientType", DababyConfigs.dababyCLIENT.trim());
            jsonObject.put("apiVersion", "v3");
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
        }
        return encryptDefault_dababy(jsonObject.toString());
    }

    //获取App配置
    public void fetchLauncherConfig(Runnable respRunnable) {
        request(
                parseDefaultParam(Paths_dababy.PATH_APP_SHOW_INFO, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                App_dababyShow_dababyCustomize.par_dababyse(((JSONObject)obj).optString("customize"));
                                //...
                                if (respRunnable != null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                            DababyUtil.stopLoading();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //获取登录验证码
    public void requestCode(String phoneNum, Runnable respRunnable) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", phoneNum.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
        }

        request(
                parseDefaultParam(Paths_dababy.PATH_SMS_CODE, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                Base_dababyApplication.getInstant().setMs_dababyg(msg);
                                Othe_dababyrsInfo.getInstance_dababy().setSendOtpCodeJson_dababy((JSONObject) obj);
                                //...
                                if (respRunnable != null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                            DababyUtil.stopLoading();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }


    //获取语音登录验证码
    public void requestCodeViaCall(String phoneNum, Runnable respRunnable) {

        j5Bu2();

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("type", "VOICE");
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
        }

        request(
                parseDefaultParam(Paths_dababy.PATH_SMS_CODE, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                Base_dababyApplication.getInstant().setMs_dababyg(msg);
                                Othe_dababyrsInfo.getInstance_dababy().setSendOtpCodeJson_dababy((JSONObject) obj);
                                //...
                                if (respRunnable != null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }


    //注册登录-有验证码
    public void usLgWithCode(String phoneNum, String verCode, Runnable respRunnable, OnLoginFailListener onLoginFailListener) {
        DababyUtil.setMobile_dababy(phoneNum);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channelCode", DababyAnalyticsUtil.INSTANCE.getInstallReferrer_dababy());
            jsonObject.put("subChannelCode", DababyAnalyticsUtil.INSTANCE.getInstallReferrer_dababy());
            jsonObject.put("code", verCode.trim());
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("notBindWalletFlag", "YES");
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_USER_LOGIN, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                if (onLoginFailListener!=null) onLoginFailListener.onLoginFail(code,msg);
                                else swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                DababyAnalyticsUtil.INSTANCE.initDevice_dababy(Base_dababyApplication.getInstant());
                                Log_dababyinInfo.par_dababyse((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //注册登录-老用户
    public void userLg_OldUser(String phoneNum, Runnable respRunnable) {
        DababyUtil.setMobile_dababy(phoneNum);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channelCode", DababyAnalyticsUtil.INSTANCE.getInstallReferrer_dababy());
            jsonObject.put("subChannelCode", DababyAnalyticsUtil.INSTANCE.getInstallReferrer_dababy());
            jsonObject.put("code", "0000");
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("directLoginFlag","YES");
            jsonObject.put("notBindWalletFlag","YES");
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_USER_LOGIN, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                DababyAnalyticsUtil.INSTANCE.initDevice_dababy(Base_dababyApplication.getInstant());
                                Log_dababyinInfo.par_dababyse((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    public void submitFb(String type, String opinion, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", type);
            jsonObject.put("opinion", opinion);
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_FEED_BACK, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //请求用户流程状态
    public void requestUsPrcs(Runnable respRunnable) {
        request(
                parseDefaultParam(Paths_dababy.PATH_USER_PROCESS, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                Pro_dababycessIn_dababyfo.parse_dababy((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //上传用户信息
    public void uploadUserPrcsInfo(String path, TreeMap<String, String> dataMap, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            for (String k : dataMap.keySet()) {
                String value = dataMap.get(k);
                jsonObject.put(k, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }

        request(
                parseDefaultParam(path, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                                //经验+5
                                Pers_dababyonalIn_dababyfo.getInstance().addExpValue(5);
                                Pers_dababyonalIn_dababyfo.getInstance().uploadUserInfo(null);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //请求额度列表
    public void requestHomeAmountList(Runnable respRunnable) {
        request(
                parseDefaultParam(PATH_LOAN_LIMIT, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                LnLi_dababymitInfo.parse_dababy((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //请求借款申请页信息/贷款详情
    public void fetchLnAplDetail(Runnable respRunnable) {
        request(
                parseDefaultParam(Paths_dababy.PATH_GET_LOAN_APPLY_DETAIL, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                LoanApplyDetail.parse_dababy((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //请求借款申请页信息/贷款详情（老客）
    public void fetchLnAplDetail_OldUser(Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("amount","0");
        }catch (Exception e){
            e.printStackTrace();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_GET_LOAN_APPLY_DETAIL_OLD_USER, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                LoanApplyDetail_OldUser.parse_dababy((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //请求借款申请页信息/贷款详情（老客）
    public void fetchLnAplDetail_OldUser(String maxAmount,String minAmount,String amount,String days,Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("maxAmount",maxAmount);
            jsonObject.put("minAmount",minAmount);
            jsonObject.put("amount",amount);
            jsonObject.put("days",days);
        }catch (Exception e){
            e.printStackTrace();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_GET_LOAN_APPLY_DETAIL_OLD_USER, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                LoanApplyDetail_OldUser.parse_dababy((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //发起借款申请
    public void aplyLn(Runnable respRunnable) {
        request(
                parseDefaultParam(Paths_dababy.PATH_LOAN_APPLY, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                DababyAnalyticsUtil.INSTANCE.addEvent_dababy(DababyIAnalyticsEvent.AT_LOAN_APPLY);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //发起借款申请（老客）
    public void aplyLn_OldUser(String amount,String days,Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("amount",amount);
            jsonObject.put("days",days);
        }catch (Exception e){
            e.printStackTrace();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_LOAN_APPLY, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                DababyAnalyticsUtil.INSTANCE.addEvent_dababy(DababyIAnalyticsEvent.AT_LOAN_APPLY);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //获取还款信息
    public void fetchRpDetail(Runnable respRunnable) {
        request(
                parseDefaultParam(Paths_dababy.PATH_REPAY_INFO, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                RpI_dababynf_dababyo.parse((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //获取还款H5（加纳、科特）
    public void odRpH5Path(String deferredFlag, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deferredFlag", deferredFlag.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_ORDER_REPAY_H5, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                Othe_dababyrsInfo.getInstance_dababy().setRpPath_dababy(((JSONObject)obj).optString("url"));
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //获取还款H5（乌干达、肯尼亚）
    public void getOdRpH5Path(String deferredFlag, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deferredFlag", deferredFlag.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_GET_ORDER_REPAY_H5, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                Othe_dababyrsInfo.getInstance_dababy().setRpPath_dababy(((JSONObject)obj).optString("repayUrl"));
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //获取钱包列表（科特、喀麦隆、塞内加尔）
    public void fetchWalletAccountList_fr(Runnable respRunnable) {
        request(
                parseDefaultParam(Paths_dababy.PATH_FETCH_BOUND_BANK_ACCOUNT, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONArray) {
                                Acc_dababyountLi_dababyst_Fr.parse_dababy((JSONArray) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //新增钱包（科特、喀麦隆、塞内）
    public void addWalletAccount_Fr(String walletMobile, String walletMobileConfirm, String walletChannel, Runnable respRunnable, boolean addBindFlag) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("walletMobile", walletMobile);
            jsonObject.put("walletMobileConfirm", walletMobileConfirm);
            jsonObject.put("walletChannel", walletChannel);
            jsonObject.put("addBindFlag", addBindFlag ? "YES":"NO");
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(Paths_dababy.PATH_BIND_BANK_ACCOUNT, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                JSONObject json = (JSONObject) obj;
                                Othe_dababyrsInfo.getInstance_dababy().setChannel_dababy(json.optString("channel"));
                                Othe_dababyrsInfo.getInstance_dababy().setAddBindFlag_dababy(json.optString("addBindFlag"));
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //删除钱包（fr）
    public void deleteWalletAccount_Fr(String ids, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ids", ids);
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }
        request(
                parseDefaultParam(PATH_DELETE_BOUND_BANK_ACCOUNT, jsonObject),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    //获取评分弹窗信息
    public void fetchRateConfig(Runnable respRunnable){
        request(
                parseDefaultParam(Paths_dababy.PATH_FETCH_STAR_WINDOW_CONFIG, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
                            if (code!=0 && !TextUtils.isEmpty(msg)) {
                                swToastBySafeWay(msg);
                                DababyUtil.stopLoading();
                            }
                            //成功响应
                            else if (obj instanceof JSONObject) {
                                Rate_dababyWind_dababyowConfig.parseData_dababy((JSONObject) obj);
                                //...
                                if (respRunnable!=null) safeRun(respRunnable);
                            }else DababyUtil.stopLoading();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

    /**
     * 保存页面显示/隐藏打点
     */
    public void uploadEventPoint(String page, String event, String remark) {
        if (DababyUtil.isFrontEmpty_dababy()||TextUtils.isEmpty(page)) return;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", DababyConfigs.dababyProduct +"_"+page+"_"+event);
            if (!TextUtils.isEmpty(remark)) {
                jsonObject.put("remark",remark);
            }
        } catch (JSONException e) {
            e.printStackTrace();
//            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
        }

        if (!DababyUtil.isFrontEmpty_dababy()){
            String commonPath = DababyUtil.getCommonPath_dababy();
            if (TextUtils.isEmpty(commonPath)) return;
            new Thread(() -> {

                Request request = new Request.Builder()
                        .url(commonPath)
                        .header("product", DababyConfigs.dababyProduct)
                        .post(RequestBody.Companion.create(parseDefaultParam(Paths_dababy.PATH_USER_BURIED_POINT, jsonObject), MediaType.Companion.parse("*/*;charset=utf-8")))
                        .build();

                Client.newCall(request).enqueue(
                        //响应
                        new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {}
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) {}
                        }
                );

            }).start();
        }
    }

    /**
     * 获取目标地区path
     */
    public void fetchTimeZoneConfig(String timeZ, Runnable respRunnable){

        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("p", DababyConfigs.dababyProduct.toLowerCase());
            jsonObject.put("tz",timeZ);
        }catch (JSONException e){
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
            DababyUtil.stopLoading();
        }

        String body = encryptDefault_dababy(jsonObject.toString());

        new Thread(() -> {
            MediaType mediaType = MediaType.Companion.parse("*/*;charset=utf-8");
            Request request = new Request.Builder()
                    .url(Paths_dababy.TimezonePath)
                    .header("product", DababyConfigs.dababyProduct)
                    .post(RequestBody.Companion.create(body, mediaType))
                    .build();
            Call call = Client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.request_fail));
                    DababyUtil.stopLoading();
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    String body = decryptDefault_dababy(Objects.requireNonNull(response.body()).string());
                    Log.w("--tz_body--", body );
                    try {
                        JSONObject responseJson = new JSONObject(body);
                        String msg = responseJson.optString("msg");
                        if (msg.equalsIgnoreCase("success")){
                            String[] data = responseJson.optString("data").split(",");
                            String front = "";
                            String mobileprefix = "";
                            if (data.length>0) front = data[0];
                            if (data.length>1) mobileprefix = data[1];
                            DababyUtil.setFront_dababy(front);
                            DababyUtil.setAreaCode_dababy(mobileprefix);
                            if (respRunnable!=null) safeRun(respRunnable);
                        }else {
                            swToastBySafeWay(msg);
                            DababyUtil.stopLoading();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
                        DababyUtil.stopLoading();
                    }
                }
            });
        }).start();
    }

    /**
     * 上传图片
     */
    public void uploadImage(String filePath_equitable, Runnable respRunnable) {
        try {
            if (!TextUtils.isEmpty(filePath_equitable)) {
                String fileSuffix_equitable = filePath_equitable.endsWith(".png") ? "image/png" : "image/jpg";
                final MediaType mediaType_equitable = MediaType.parse(fileSuffix_equitable);
                final File imageFile_equitable = new File(filePath_equitable);
                final String uplodPaths_equitable = Paths_dababy.BasePath + DababyUtil.getFront_dababy()+"/"+ PATH_UPLOAD_IMAGE_FILE;
                if (imageFile_equitable.isFile()) {
                    RequestBody imageBody = RequestBody.create(mediaType_equitable, imageFile_equitable);

                    RequestBody requestBody_equitable = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("image", imageFile_equitable.getName(), imageBody)
                            .build();

                    Request request_equitable = new Request.Builder()
                            .url(uplodPaths_equitable)
                            .post(requestBody_equitable)
                            .build();
                    Call call = Client.newCall(request_equitable);
                    //响应
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.request_fail));
                            DababyUtil.stopLoading();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String body = Objects.requireNonNull(response.body()).string();
                            try {
                                JSONObject responseJson = new JSONObject(body);
                                int code = Integer.parseInt(responseJson.optString("code"));
                                String msg = responseJson.optString("msg");
                                Object obj = responseJson.opt("obj");
                                //失败响应
                                if (code!=0) {
                                    swToastBySafeWay(msg);
                                    return;
                                }
                                //成功响应
                                if (obj instanceof JSONObject) {
                                    Othe_dababyrsInfo.getInstance_dababy().setImgPath_dababy(((JSONObject)obj).optString("url"));
                                    //...
                                    if (respRunnable!=null) safeRun(respRunnable);
                                }else DababyUtil.stopLoading();
                            } catch (Exception e) {
                                e.printStackTrace();
                                swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.ocr_tips_img_upload_faile));
                            }
                        }
                    });
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存用户资料
     */
    public void uploadUserInfo(String jsonStr,Runnable respRb) {
        if (DababyUtil.isFrontEmpty_dababy()) return;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userInfo",jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
            swToastBySafeWay(Base_dababyApplication.getInstant().getString(R.string.error));
        }

        if (!DababyUtil.isFrontEmpty_dababy()){
            String commonPath = DababyUtil.getCommonPath_dababy();
            if (TextUtils.isEmpty(commonPath)) return;
            new Thread(() -> {

                Request request = new Request.Builder()
                        .url(commonPath)
                        .header("product", DababyConfigs.dababyProduct)
                        .post(RequestBody.Companion.create(parseDefaultParam(Paths_dababy.PATH_SAVE_DATA, jsonObject), MediaType.Companion.parse("*/*;charset=utf-8")))
                        .build();

                Client.newCall(request).enqueue(
                        //响应
                        new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                DababyUtil.stopLoading();
                            }
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) {
                                if (respRb!=null) respRb.run();
                            }
                        }
                );

            }).start();
        }
    }

    /**
     * 获取用户资料
     */
    public void requestUserInfo(Runnable respRunnable){
        request(
                parseDefaultParam(Paths_dababy.PATH_GET_DATA, null),
                new ResponseListener_dababy() {
                    @Override
                    public void onResponse(JSONObject responseJson) {
                        try {
                            int code = Integer.parseInt(responseJson.optString("code"));
                            String msg = responseJson.optString("msg");
                            Object obj = responseJson.opt("obj");
                            //失败响应
//                            if (code!=0) {}
                            //成功响应
                            if (code==0&&obj instanceof JSONObject) {
                                Rate_dababyWind_dababyowConfig.parseData_dababy((JSONObject) obj);
                                //...
                                Pers_dababyonalIn_dababyfo.parse_dababy(((JSONObject)obj).optString("userInfo"));
                                if (respRunnable!=null) safeRun(respRunnable);
                                else {
                                    DababyUtil.stopLoading();
                                }
                            }else {
                                Pers_dababyonalIn_dababyfo.parse_dababy("");
                                DababyUtil.stopLoading();
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onResponseFail() {
                        DababyUtil.stopLoading();
                    }
                }
        );
    }

}
